---
title: "Replication Code for Does the Losing Side Lose the Democratic Faith?"
author: "Yiming Wang"
date: "2022/1/13"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(scipen=999)
```


```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}
library(skimr)
library(robustHD)
library(stargazer)
library(tidyverse)
library(ggplot2)
library(sjPlot)
library(sjmisc)
library(ggpubr)
library(grid)

df <- read.csv("prepared data.csv")

# filter out who did not vote for Biden and Trump
df <- subset(df, vote == "Joe Biden" | vote == "Donald Trump")
df$ideology <- df$pre_ideology

df$vote <- relevel(factor(df$vote), ref = "Joe Biden")
df$race <- relevel(factor(df$race), ref = "White")
```

## Table 1. Democratic values before and after the election (general voters)

```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}

# majority rule, pre-election wave vs. post-election wave
t_majority_rule <- t.test(df$majority_rule,df$pre_majority_rule,
               alternative = "greater",
               paired = TRUE) 
t_majority_rule

# participation, pre-election wave vs. post-election wave
t_participation <- t.test(df$participation,df$pre_participation,
               alternative = "greater",
               paired = TRUE) 
t_participation


# equality, pre-election wave vs. post-election wave
t_equality <- t.test(df$equality,df$pre_equality,
               alternative = "greater",
               paired = TRUE) 
t_equality


```



## Table 2. Democratic values before and after the election (by voter groups)

```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}


# Biden's voters

## majority rule, pre-election wave vs. post-election wave
t_majority_rule_Biden <- t.test(df$majority_rule[df$vote == "Joe Biden"], df$pre_majority_rule[df$vote == "Joe Biden"],alternative = "greater",
                  paired = TRUE)
t_majority_rule_Biden

## participation, pre-election wave vs. post-election wave
t_participation_Biden <- t.test(df$participation[df$vote == "Joe Biden"], df$pre_participation[df$vote == "Joe Biden"],alternative = "greater",
                  paired = TRUE) 
t_participation_Biden

## equality, pre-election wave vs. post-election wave
t_equality_Biden <- t.test(df$equality[df$vote == "Joe Biden"], df$pre_equality[df$vote == "Joe Biden"],alternative = "greater",
                  paired = TRUE) 
t_equality_Biden



# Trump's voters

## majority rule, pre-election wave vs. post-election wave
t_majority_rule_Trump <- t.test(df$majority_rule[df$vote == "Donald Trump"], df$pre_majority_rule[df$vote == "Donald Trump"],alternative = "greater",
                  paired = TRUE)
t_majority_rule_Trump

## participation, pre-election wave vs. post-election wave
t_participation_Trump <- t.test(df$participation[df$vote == "Donald Trump"], df$pre_participation[df$vote == "Donald Trump"],alternative = "greater",
                  paired = TRUE) 
t_participation_Trump

## equality, pre-election wave vs. post-election wave
t_equality_Trump <- t.test(df$equality[df$vote == "Donald Trump"], df$pre_equality[df$vote == "Donald Trump"],alternative = "greater",
                  paired = TRUE) 
t_equality_Trump

```


## Table 3. Main effects of partisan news flow on democratic values


```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}
m1_1 <- lm(change_majority_rule ~ vote + partisan_news_flow + pre_majority_rule + ideology + gender + age + race + education + income, data = df)
m1_2 <- lm(change_participation ~ vote + partisan_news_flow + pre_participation + ideology + gender + age + race + education + income, data = df)
m1_3 <- lm(change_equality  ~ vote + partisan_news_flow + pre_equality + ideology + gender + age + race + education + income, data = df)


stargazer(m1_1,m1_2,m1_3,
          single.row = FALSE,
          type = "html",
          digits = 2,
          star.char = c("+","*", "**", "***"),
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001),
          notes = c("+ p<0.1; * p<0.05; ** p<0.01; *** p<0.001"),  
          no.space = TRUE, ci = TRUE
          )

```


## Table 4. Interaction effects of partisan news flows and voter’s type on democratic values

```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}

m2_1 <- lm(change_majority_rule ~ vote*partisan_news_flow + pre_majority_rule + ideology + gender + age + race + education + income , data = df)
m2_2 <- lm(change_participation ~ vote*partisan_news_flow + pre_participation + ideology + gender + age + race + education + income, data = df)
m2_3 <- lm(change_equality  ~ vote*partisan_news_flow + pre_equality + ideology + gender + age + race + education + income, data = df)

stargazer(m2_1,m2_2,m2_3,
          single.row = FALSE,
          type = "html",
          digits = 2,
          star.char = c("+","*", "**", "***"),
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001),
          notes = c("+ p<0.1; * p<0.05; ** p<0.01; *** p<0.001"),  
          no.space = TRUE, ci = TRUE
          )

```


## Figure 1. Interaction plots of voter type and partisan news flow on participation value

```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}


# pre-election wave
p1 <- ggplot(df,
             aes(x = pre_partisan_news_flow,
                 y = pre_participation,
                 color = vote)) +
  theme_bw() + 
  labs(x = "Partisan news flow (before the election)",
       y = "Pre-election",
       color = "Vote choice") + 
  geom_point(alpha = .3, size = .9) +
  geom_smooth(method = "lm") +
  scale_color_manual(values=c("#0072B5FF","#BC3C29FF"))+ 
  theme(legend.position="none")


# post-election wave
p2 <- ggplot(df,
             aes(x = partisan_news_flow,
                 y = participation,
                 color = vote)) +
  theme_bw() + 
  labs(x = "Partisan news flow (during the election)",
       y = "Post-election",
       color = "Vote choice") + 
  geom_point(alpha = .3, size = .9) +
  geom_smooth(method = "lm") +
  scale_color_manual(values=c("#0072B5FF","#BC3C29FF"))+ 
  theme(legend.position="none")



# change
p3 <- ggplot(df,
             aes(x = partisan_news_flow,
                 y = change_participation,
                 color = vote)) +
  theme_bw() + 
  labs(x = "Partisan news flow (during the election)",
       y = "Change during the election",
       color = "Vote choice") + 
  geom_point(alpha = .3, size = .9) +
  geom_smooth(method = "lm") +
  scale_color_manual(values=c("#0072B5FF","#BC3C29FF"))


figure1 <- ggarrange(ggarrange(p1, p2, nrow = 2), p3, ncol = 2, common.legend = TRUE, legend="bottom") 

figure1 <- annotate_figure(figure1, left = textGrob("Valuing participation", rot = 90, vjust = 1, gp = gpar(cex = 1.3)))


ggsave(figure1, 
       filename = "Figure 1.jpg",
       device = "jpg",
       height = 6, width = 10, units = "in")

```